package com.zjr.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjr.domain.Log;
import com.zjr.domain.User;
import com.zjr.service.LogService;

/**
 * Servlet implementation class LogServlet
 */
@WebServlet("/LogServlet")
public class LogServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LogServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String path = request.getContextPath();
		
		LogService ls = null;
		Log log = null;
		String flag = request.getParameter("flag");
		if(flag.equals("add")){
			ls = new LogService();
			log = new Log();
			log.setUsername(request.getParameter("username"));
			log.setPlace(request.getParameter("place"));
			log.setDuration(Float.parseFloat(request.getParameter("duration")));
			log.setSubmittime(request.getParameter("submittime"));
			log.setState(Integer.parseInt(request.getParameter("state")));
			log.setContext(request.getParameter("content"));
			if(ls.addLog(log)){
				response.sendRedirect(path+"/log/success.jsp");
			}else{
				response.sendRedirect(path+"/log/error.jsp");
			}
		}else if(flag.equals("query")){
			ls = new LogService();
			HttpSession userSession = request.getSession();
			User user = (User)userSession.getAttribute("user");
			ArrayList<Log> logs = null;
			if(user.getPosition() < 1){
				logs = ls.queryLogsByUsername(user.getUsername());
			}else{
				logs = ls.queryAllLogs();
			}
			if(logs == null){
				response.sendRedirect(path+"/log/no_log.jsp");
			}else{
				request.setAttribute("logs", logs);
				request.getRequestDispatcher("/log/log_list.jsp")
					.forward(request, response);	
			}
		}else if(flag.equals("find")){
			ls = new LogService();
			int lid = Integer.parseInt(request.getParameter("lid"));
			log = ls.findLogById(lid);
			request.setAttribute("log", log);
			request.getRequestDispatcher("/log/log_modify.jsp")
				.forward(request, response);
		}else if(flag.equals("modify")){
			ls = new LogService();
			log = new Log();
			log.setLid(Integer.parseInt(request.getParameter("lid")));
			log.setState(Integer.parseInt(request.getParameter("state")));
			log.setContext(request.getParameter("content"));
			if(ls.modifyLog(log)){
				response.sendRedirect(path+"/log/success.jsp");
			}else{
				response.sendRedirect(path+"/log/error.jsp");
			}
		}else if(flag.equals("delete")){
			ls = new LogService();
			int lid = Integer.parseInt(request.getParameter("lid"));
			ls.deleteLogByLid(lid);
			response.sendRedirect(path+"/log/success.jsp");
		}	
	}

}
